﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace DrillOnline.Controllers
{
    [Authorize()]
    public class PlayerController : _BaseController
    {
        //
        // GET: /Player/
        //GET:			/Player/			wait,sesioninfo,joinsession
        [AllowAnonymous]
        public ActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return Redirect("~/Player/Login");
            }
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }


            return View(cs);
        }
        //GET: 	/Player/Login				groupAndUserList,login        
        [AllowAnonymous]
        public ActionResult Login()
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s=>s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }
            //if (!cs.IsReady) { return Redirect("Index"); }

            var m = cs;

            return View(m);
        }
        //POST:	/Player/Login				groupAndUserList,login
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(string user, string pwd)
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }

            var u = cs.ActorGroups.Where(v=>v.Actors!=null).SelectMany(v => v.Actors).FirstOrDefault(v => v.ActorKey.ToLower() == user.ToLower() && v.Password == pwd);
            if (null != u)
            {
                System.Web.Security.FormsAuthentication.SetAuthCookie(u.ActorKey, false);
                return Redirect("~/Player/Index");
            }
            else
            {
                ModelState.AddModelError("loginfailed", new Services.SiteException("登录失败."));
                return View(cs);
            }
        }
        //GET:			/Player/CtrlPanel			eventlist,media,pin,ZhiShu,reply
        [HttpGet]
        public ActionResult CtrlPanel()
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }

            var m = cs;
            return View(m);
        }
        //POST:			/Player/CtrlPanel			eventlist,media,pin,ZhiShu,reply
        //[HttpGet]
        //public ActionResult CtrlPanel(string m)
        //{
        //    return View();
        //}

        //GET:			/Player/Summary				wait,viewsummary
        public ActionResult Summary()
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }


            var m = DB.TrainningEvaluations.Where(v => v.TrainingSession == cs.Session && v.SubjectEvaluationRule.Group == ScenariosUser.GroupKey);
            return View(m);
        }

    }
}
